package com.awanp3.nimbus.remote.context;

import com.awanp3.nimbus.remote.api.domain.RemoteWmsPoInfo;
import com.awanp3.nimbus.remote.api.model.FetchOrderParam;
import com.awanp3.nimbus.remote.api.model.PostOrderParam;
import com.awanp3.nimbus.remote.api.model.PostOrderResult;
import com.awanp3.nimbus.remote.strategy.WmsPoInfoStrategy;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class WmsPoInfoContext {

    private static final String BASE_STRATEGY_NAME = "PoInfo";

    private final Map<String, WmsPoInfoStrategy> strategyMap = new ConcurrentHashMap<>();

    public WmsPoInfoContext(Map<String, WmsPoInfoStrategy> strategyMap) {
        this.strategyMap.putAll(strategyMap);
    }

    /**
     * 下载订单
     *
     * @param param 参数
     * @return List<RemoteWmsPoInfo>
     */
    public List<RemoteWmsPoInfo> fetch(FetchOrderParam param){
        WmsPoInfoStrategy strategy = strategyMap.get(param.getCompanyCode() + BASE_STRATEGY_NAME);
        if (strategy != null) {
            return strategy.fetch(param);
        } else {
            throw new IllegalArgumentException("公司：" + param.getCompanyCode() + "不支持PO下载");
        }
    }

    /**
     * 过账
     *
     * @param param 过账参数
     * @return PostOrderResult
     */
    public PostOrderResult post(PostOrderParam param){
        WmsPoInfoStrategy strategy = strategyMap.get(param.getCompanyCode() + BASE_STRATEGY_NAME);
        if (strategy != null) {
            return strategy.post(param);
        } else {
            throw new IllegalArgumentException("公司：" + param.getCompanyCode() + "不支持PO过账");
        }
    }
}


